package com.hxd.otc.module.coin_deal.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.coin_deal.adapter.CoinDealRecoderAdapter;
import com.hxd.otc.module.coin_deal.bean.CoinDealRecoderBean;
import com.hxd.otc.module.coin_deal.contract.CoinDealRecoderContract;
import com.hxd.otc.module.coin_deal.presenter.CoinDealRecordPresenter;
import com.hxd.otc.module.order.activity.OrderFinishedActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

public class CoinDealListActivity extends BaseActivity<CoinDealRecordPresenter> implements CoinDealRecoderContract.View {

    int pageNo = 1;
    int pageSize = 15;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;
    View notDataView;
    TextView tvRefresh;

    CoinDealRecoderAdapter adapter;

    private boolean isRefresh;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coin_deal_list;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;

                mPresenter.loadData(++pageNo, pageSize);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                pageNo = 1;
                mPresenter.loadData(pageNo, pageSize);
            }
        });

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);

        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });

        initAdapter();
    }

    @Override
    protected void initData() {
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new CoinDealRecoderAdapter();
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(OrderFinishedActivity.COINDEALRECODERBEAN, (CoinDealRecoderBean.DataBean) adapter.getData().get(position));
                    $startActivity(OrderFinishedActivity.class, bundle, false);
                }
            });
        }

        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected CoinDealRecordPresenter createPresenter() {
        return new CoinDealRecordPresenter(this);
    }

    @Override
    public void onSetAdapter(List<CoinDealRecoderBean.DataBean> list) {

        if (isRefresh) {
            mRefreshLayout.setEnableLoadMore(true);
            if (list != null && list.size() <= 0) {
                adapter.setEmptyView(notDataView);
            }
            adapter.setNewData(list);
        } else {
            if (list == null || list.size() < pageNo) {
                mRefreshLayout.setEnableLoadMore(false);
            } else {
                mRefreshLayout.setEnableLoadMore(true);
                adapter.addData(list);
            }

        }
    }

    @Override
    public void onShowNoMore() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }
}
